package com.xhwl.centiotdata.suppliersystem.guangtuo.alarmhost.websocket.handle.event;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.xhwl.centiotdata.service.cent.IAlarmHostCentService;
import com.xhwl.centiotdata.suppliersystem.guangtuo.alarmhost.websocket.handle.AlarmHostNotifyHandle;
import com.xhwl.centiotdata.suppliersystem.guangtuo.alarmhost.websocket.handle.AlarmHostNotifyHandleFactory;
import com.xhwl.common.pojo.ResultJson;
import com.xhwl.common.pojo.cent.alarmhost.DefAreaNoticeRecord;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;

/**
 * @author zyz
 * @date 2023-04-26
 * 报警主机报警事件通知处理
 */
@Slf4j
@Service
public class AlarmHostAlarmEventHandle extends AlarmHostNotifyHandle {
    private static final String TOPIC = "/ws/alarmhost/v2/notification/event/alarm";

    @Resource
    private IAlarmHostCentService iAlarmHostCentService;

    @Override
    public void afterPropertiesSet() throws Exception {
        AlarmHostNotifyHandleFactory.register(TOPIC, this);
    }

    @Override
    public void handler(String status,String data) {
        log.info("websocket-报警主机报警事件通知处理  TOPIC={},status={},data={}", TOPIC, status, data);
        JSONObject jsonObject = JSON.parseObject(data);
        DefAreaNoticeRecord record = new DefAreaNoticeRecord();
        record.setObjId((String) jsonObject.get("objID"));
        record.setAlarmOp((String) jsonObject.get("alarmOp"));
        record.setAlarmType((String) jsonObject.get("alarmType"));
        record.setAlarmTime((String) jsonObject.get("alarmTime"));
        record.setObjNo((Integer) jsonObject.get("objNo"));
        ResultJson checkEventRecordExistResult = iAlarmHostCentService.checkEventRecordExist(record);
        if (!checkEventRecordExistResult.getState()){
            ResultJson resultJson = iAlarmHostCentService.addAlarmNotifyRecord(record);
            log.info("报警事件通知添加状况TOPIC：{},status={},addFlag={}", TOPIC, status, resultJson.getState());
        }else{
            log.info("当期报警事件通知数据重复推送!");
        }
    }
}
